﻿using System;
using NLog;
using NLog.Config;
using NLog.Targets;

namespace SsoaFramework.Service
{
    public class LogService
    {
        private readonly string serviceName;
        private string address;
        private Logger logger;

        public LogService(string serviceName, string address = "http://localhost:9900/")
        {
            this.serviceName = serviceName;
            this.address = address;

            LoggingConfiguration loggingConfiguration = new LoggingConfiguration();
            var target = new LogReceiverWebServiceTarget()
            {
                EndpointAddress = address,
                IncludeEventProperties = true,
                ClientId = "${guid:cached=true}"
            };
            loggingConfiguration.AddTarget("receiver", target);
            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, target));

            //console
            ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
            loggingConfiguration.AddTarget("ColoredConsole",consoleTarget);
            loggingConfiguration.LoggingRules.Add(new LoggingRule("*",LogLevel.Trace,consoleTarget));
            //
            LogManager.Configuration = loggingConfiguration;
            logger = LogManager.GetLogger(serviceName);
        }

        public void Debug(string message, Guid guid)
        {
            Log(message, guid, LogLevel.Debug);
        }
        public void Error(string message, Guid guid)
        {
            Log(message, guid, LogLevel.Error);
        }
        public void Fatal(string message, Guid guid)
        {
            Log(message, guid, LogLevel.Fatal);
        }
        public void Info(string message, Guid guid)
        {
            Log(message, guid, LogLevel.Info);
        }
        public void Trace(string message, Guid guid)
        {
            Log(message, guid, LogLevel.Trace);
        }
        public void Warn(string message, Guid guid)
        {
            Log(message, guid, LogLevel.Warn);
        }
        public void Debug(string message)
        {
            Log(message, Guid.Empty, LogLevel.Debug);
        }
        public void Error(string message)
        {
            Log(message, Guid.Empty, LogLevel.Error);
        }
        public void Fatal(string message)
        {
            Log(message, Guid.Empty, LogLevel.Fatal);
        }
        public void Info(string message)
        {
            Log(message, Guid.Empty, LogLevel.Info);
        }
        public void Trace(string message)
        {
            Log(message, Guid.Empty, LogLevel.Trace);
        }
        public void Warn(string message)
        {
            Log(message, Guid.Empty, LogLevel.Warn);
        }

        private void Log(string message, Guid guid, LogLevel logLevel)
        {
            var log = new LogEventInfo(logLevel, serviceName, message);
            log.Properties.Add("Guid", guid);
            logger.Log(log);
        }
    }
}
